//https://leetcode.cn/problems/remove-linked-list-elements/submissions/539610123/

struct ListNode* removeElements(struct ListNode* head, int val) {

    struct ListNode* prev = NULL;
    struct ListNode* cur = head;

    while (cur != NULL)
    {
        //没有遇到val
        if (cur->val != val)
        {
            prev = cur;
            cur = cur->next;
        }

        else
        {
            //要删除的节点为头节点
            if (prev == NULL)
            {
                head = cur->next;
                free(cur);
                cur = head;   //改变cur的位置
            }

            else
            {
                prev->next = cur->next;
                free(cur);
                cur = prev->next;
            }
        }
    }

    return  head;
}